package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2025-03-30
 * Time:21:28
 */
public class LoginPage extends Utils {
    public static String url="http://127.0.0.1:9580/sign-in.html";
    public LoginPage() {
        super(url);
    }
    //查看是否有登录页面
    public void LoginPageRight() throws InterruptedException {
        //通过查看页面元素是否存在来检查页面加载是否成功
        driver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.card.card-md > div"));
        //页面上是否出现登录文字
        driver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.card.card-md > div > h2"));
        Thread.sleep(3000);
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#submit"));

    }
    //登录成功
    public void LoginSuc() throws InterruptedException {
        //清除原有的用户名和密码
        driver.navigate().refresh();
        Thread.sleep(3000);
        driver.findElement(By.cssSelector("#username")).sendKeys("xiaoyuann");
        driver.findElement(By.cssSelector("#password")).sendKeys("1234");
        driver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(3000);
        //检查点击确定之后是否登陆成功，进入会话列表页
        String expect="首页";
        String text=driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        assert expect.equals(text);
        Thread.sleep(3000);
    }
    //登录失败
    //1.用户名，密码错误
    public void LoginFail() throws InterruptedException {
        //清除原有的用户名和密码
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#username")).sendKeys("wangyuan11");
        driver.findElement(By.cssSelector("#password")).sendKeys("1234");
        driver.findElement(By.cssSelector("#submit")).click();
        //等待弹窗
        Thread.sleep(3000);
        //还在原来页面
        String except="登录";
        String res=driver.findElement(By.cssSelector("#submit")).getText();
        assert res.equals(except);
        Thread.sleep(3000);
        driver.navigate().refresh();
        Thread.sleep(3000);
    }
    //2.用户名和密码为空
    public void LoginFailNull() throws InterruptedException {
        //清除原有的用户名和密码
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#submit")).click();
        //等待弹窗
        Thread.sleep(3000);
        //出现用户名密码为空提示
        String except="用户名不能为空";
        String res=driver.findElement(By.cssSelector("#signInForm > div.mb-3 > div")).getText();
        assert res.equals(except);
        String except1="密码不能为空";
        String res1=driver.findElement(By.cssSelector("#signInForm > div.mb-2 > div > div")).getText();
        assert res1.equals(except1);
        Thread.sleep(3000);
        driver.navigate().refresh();
        Thread.sleep(3000);
    }
}
